package org.hackystat.sdsa.analyzer.control.tokenizer;

import java.util.ArrayList;
import java.util.List;

import org.hackystat.sdsa.analyzer.model.Episode;
import org.hackystat.sdsa.analyzer.model.activity.Activity;

/**
 * The super class of all tokenizers.
 * 
 * @author hongbing
 *
 */
public abstract class Tokenizer {
  /**
   * Tokenize the development stream activities using buffer transition as tokens. 
   * 
   * @param activityList Activity list.
   * @return Episodes tokenized.
   */
  public List<Episode> execute(List<Activity> activityList) {
    List<Episode> episodeList = new ArrayList<Episode>();
    
    if (activityList == null) {
      return episodeList;
    }

    int start = 0; 
    while (start < activityList.size()) {
      // Gets next token
      int nextTestPass = this.lookAhead(activityList, start);
      // Make a new episode
      Episode episode = new Episode();
      for (; start <= nextTestPass; start++) {
        Activity activity = activityList.get(start);
        episode.addActivity(activity);
      }
      
      episodeList.add(episode);
    }

    return episodeList;
  }
  
  /**
   * Finds next token activity starting from the given position.
   * 
   * @param activityList Activity list.
   * @param start Start searching index.
   * @return Index of next token.
   */
  public abstract int lookAhead(List<Activity> activityList, int start);
}
